import Socket from '@/websocket/index'
import {useEquipmentInfoStore} from '@/stores/equipment-info-store'

export default () => {
  let wbSocket: any
  const store = useEquipmentInfoStore()
  const closeWebsocket = () => {
    wbSocket && wbSocket.destroy()
  }
  const recevierMsg = (e: MessageEvent) => {
    store.setEquipmentInfo(JSON.parse(e.data))
  }
  const initSokect = () => {
    if (location.href.indexOf('/login/') > -1) return
      const token = localStorage.getItem('token')
      wbSocket = new Socket({ url: `ws://127.0.0.1:8081/login?token=${token}`, messageCb: recevierMsg })
  }
  initSokect()
  window.addEventListener('unload', () => {
    wbSocket && wbSocket.destroy()
  })
  return {
    closeWebsocket,
    initSokect
  }
}


